package com.windfone.web.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletOutputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.windfone.common.FileTool;

public class UploadAction extends ActionSupport {
	private File file;
	private String fileFileName;
	private String fileContentType;
	private String uploadFolder;
	private String returnPic;

	public String getReturnPic() {
		return returnPic;
	}

	public void setReturnPic(String returnPic) {
		this.returnPic = returnPic;
	}

	public String getUploadFolder() {
		return uploadFolder;
	}

	public void setUploadFolder(String uploadFolder) {
		this.uploadFolder = uploadFolder;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	@Override
	public String execute() throws Exception {
//		System.out.println( "===1=="+ServletActionContext.getRequest().getHeader("reference") );
		ServletOutputStream out = ServletActionContext.getResponse().getOutputStream();
		returnPic = "";
		boolean sign = true;
		String root = "http://"+ServletActionContext.getRequest().getContextPath()+":"+ ServletActionContext.getRequest().getRemotePort();
		InputStream is = null;
		OutputStream os = null;
		if ( file!=null &&  file.exists() ){
			try {
				is = new FileInputStream(file);
				// 2.构建一个上传文件路径
				 root = ServletActionContext.getServletContext().getRealPath("/");
				// 3.获得一个本地文件
				root = root+"upload"+File.separator	+ uploadFolder+File.separator;
				File tmp = new File(root);
				if ( !tmp.exists())
					tmp.mkdirs();
				
				root = FileTool.getFolderPath(root, 0,false);
				File diskFile = new File(root, this.getFileFileName());
				returnPic = diskFile.getAbsolutePath();
				int begin = returnPic.indexOf("upload");
				returnPic = returnPic.substring(begin, returnPic.length() );
				returnPic = returnPic.replace("\\", "/");
				os = new FileOutputStream(diskFile);
				byte[] buffer = new byte[400];
				int length = 0;
				while ((length = is.read(buffer)) > 0) {
					os.write(buffer, 0, length);
				}
			}catch(Exception e){
				e.printStackTrace();
				sign = false;
			} finally {
				if (is !=null )
					is.close();
				if (os !=null )
					os.close();
			}
		}
		if(sign==true)
		{
			System.out.println("returnPic:"+returnPic);
			out.println("<script>parent.callback('upload file success|"+ returnPic +"')</script>");
		}else{
			out.println("<script>parent.callback('upload file error')</script>");
		}
		return null;
	}
	
	public static void main(String args[]){
		String str="upload\\promotions\\1\\1\\1\\2010082814284935063.jpg";
		str = str.replace("\\", "/");
		System.out.println( str);
 
	}
}
